package com.canbot.u05.receivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ActivityUtil;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.Utils;

/**
 * 接受来自其他app发送过来的广播信息
 *
 * @author xiaowei
 */
public class VoiceResultEventReceiver extends BroadcastReceiver {

        private static VoiceResultEventReceiver instance;

        private Context mContext;

        private static final String TAG = "VoiceResultEventReceiver";

        public VoiceResultEventReceiver(Context mContext) {
                this.mContext = mContext;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
                if (intent != null) {
                        String action = intent.getAction();
                        if (ReceiverSocketAction.MSG_VOICE_RECOGNIZER_RESULT.equals(action)) {
                                String result = intent.getStringExtra(IntentKey.VOICE_RESULT);
                                if (result == null || result.isEmpty()) {
                                        return;
                                }
                                Logger.d(TAG, "voice result = " + result);
                                // String result = null;

                                if (ActivityUtil.getTopActivityName(mContext).equals("com.canbot.u05.update.CheckUpdateActivity")) {
                                        Logger.d(TAG, "topActivityName is CheckUpdateActivity ");
                                        return;
                                }

                                if (result != null) {
                                        if (Utils.matchString(result,
                                                new String[]{"我比你猜"})) {
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
//                        MsgSendUtils.sendStringMsg(MsgType.SEND_START_RECOGNIZER, "ok");//
                                                gotoOtherApp("com.uurobot.u05iplayuguess");
                                                return;
                                        }
                                        else if (Utils.matchString(result,
                                                new String[]{"剪刀石头布"})) {
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
//                        MsgSendUtils.sendStringMsg(MsgType.SEND_START_RECOGNIZER, "ok");//
                                                gotoOtherApp("com.example.finger_guessinggame");
                                                return;
                                        }
                                        else if (Utils.matchString(result,
                                                new String[]{"拍照"})) {
//                        MsgSendUtils.sendStringMsg(MsgType.SEND_START_RECOGNIZER, "ok");//
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
                                                gotoOtherApp("com.jsg.photograph");
//                        MsgSendUtils.sendStringMsg(MsgType.SEND_MSGTYPE_PLAY_TTS,  "@@:好的，正在为您打开相机！");
                                                return;
                                        }
                                        else if (Utils.matchString(result,
                                                new String[]{"电台"})) {
//                        MsgSendUtils.sendStringMsg(MsgType.SEND_START_RECOGNIZER, "ok");//
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
                                                gotoOtherApp("com.myroobt.broadcasting");
                                                MsgSendUtils.sendStringMsg(MsgType.PLAY_TTS, "@@:好的，正在为您打开电台！");
                                                return;
                                        }
                                        else if (Utils.matchString(result,
                                                new String[]{"修养"})) {
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
                                                gotoOtherApp("com.canbot.mooddiscern");
                                                return;
                                        }
                                        else if (Utils.matchString(result,
                                                new String[]{"健康管理"})) {
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
                                                gotoOtherActivity("com.unisrobot.jtjkzx2", "com.unisrobot.jtjkzx2.MainActivity");
                                                return;
                                        }
                                        else if (Utils.matchString(result, new String[]{
                                                "测量血压", "我要测血压", "我要量血压", "血压"})) {
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
                                                // 跳转到测血压界面
                                                gotoOtherActivity("com.unisrobot.jtjkzx2", "com.unisrobot.jtjkzx2.BloodPressureActivity");
                                                return;
                                        }
                                        else if (Utils.matchString(result, new String[]{
                                                "测量体重", "我要测体重", "我要量体重", "体重"})) {
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
                                                // 跳转到测体重界面
                                                gotoOtherActivity("com.unisrobot.jtjkzx2", "com.unisrobot.jtjkzx2.WeightActivity");
                                                return;
                                        }
                                        else if (Utils.matchString(result, new String[]{
                                                "测量体温", "我要测体温", "我要量体温", "体温"})) {
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
                                                // 跳转到测体温界面
                                                gotoOtherActivity("com.unisrobot.jtjkzx2", "com.unisrobot.jtjkzx2.TiWenActivity");
                                                return;
                                        }
                                        else if (Utils.matchString(result, new String[]{
                                                "测量血糖", "我要测血糖", "我要量血糖", "血糖"})) {
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
                                                // 跳转到测血糖界面
                                                gotoOtherActivity("com.unisrobot.jtjkzx2", "com.unisrobot.jtjkzx2.SugarActivity");
                                                return;
                                        }
                                        else if (Utils.matchString(result, new String[]{
                                                "测量血氧", "我要测血氧", "我要量血氧", "血氧"})) {
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
                                                // 跳转到测血氧界面
//                        playSound("/jtjkzx/weikaifa.mp3",
//                                MsgType.PLAY_SOUND);
                                                return;
                                        }
                                        else if (Utils.matchString(result, new String[]{
                                                "测量脂肪", "我要测脂肪", "我要量脂肪", "脂肪"})) {
//                                                MsgSendUtils.sendStringMsg(MsgType.OPEN_PAD_APP, "ok");//
//                        playSound("/jtjkzx/weikaifa.mp3",
//                                MsgType.PLAY_SOUND);
                                                return;
                                        }
                                        else {
                                                //lijie 对快速注册有影响，先注掉
                                                //MsgSendUtils.sendStringMsg(MsgType.START_RECOGNIZER, "ok");//
                                        }
                                }
                        }
                }
        }


        /**
         * 注册广播
         */
        public void registerBR() {
                IntentFilter filter = new IntentFilter();
                filter.addAction(ReceiverSocketAction.MSG_VOICE_RECOGNIZER_RESULT);
                mContext.registerReceiver(this, filter);
        }

        /**
         * 反注册广播
         */
        public void unRegisterBR() {
                try {
                        mContext.unregisterReceiver(this);
                }
                catch (Exception e) {

                }
        }

        /**
         * 跳转到其他应用的界面
         *
         * @param packageName
         * @param className
         */
        private void gotoOtherActivity(String packageName, String className) {
                Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
                if (intent != null) {
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setClassName(packageName,
                                className);
                        mContext.startActivity(intent);
                }
                else {
                        Logger.d(TAG, "该App没有安装");
                }
        }

        /**
         * 跳转到其他应用的界面
         *
         * @param packageName
         * @param className
         */
        private void gotoOtherApp(String packageName) {
                Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
                if (intent != null) {
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                }
                else {
                        Logger.d(TAG, "该App没有安装");
                }
        }

        private void closeOtherApp(String closeAction) {
                if (closeAction != null) {
                        Intent intent = new Intent(closeAction);
                        mContext.startActivity(intent);
                }
        }

        // 跳转到本地相应界面
        private void gotoLocalActivity(Class class1) {
                Intent intent = new Intent(mContext, class1);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
        }


//    public void playSound(String path, int msgType) {
//        Intent intent = new Intent(ReceiverAPPAction.START_PLAY_SOUND);
//        intent.putExtra(IntentKey.SOUND_PATH, path);
//        intent.putExtra(IntentKey.COMPLETED_DO, IntentContent.RECODER);
//        intent.putExtra(IntentKey.MSG_TYPE, msgType);
//        mContext.sendBroadcast(intent);
//    }
}
